home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / tcl / dist6.3 / tests / open.test < prev    next >
Encoding:
Text File  |  1992-03-25  |  16.1 KB  |  512 lines

  1. # Commands covered:  open, close, gets, puts, read, seek, tell, eof, flush
  2. #
  3. # This file contains a collection of tests for one or more of the Tcl
  4. # built-in commands.  Sourcing this file into Tcl runs the tests and
  5. # generates output for errors.  No output means no errors were found.
  6. #
  7. # Copyright 1991 Regents of the University of California
  8. # Permission to use, copy, modify, and distribute this
  9. # software and its documentation for any purpose and without
  10. # fee is hereby granted, provided that this copyright notice
  11. # appears in all copies.  The University of California makes no
  12. # representations about the suitability of this software for any
  13. # purpose.  It is provided "as is" without express or implied
  14. # warranty.
  15. #
  16. # $Header: /user6/ouster/tcl/tests/RCS/open.test,v 1.12 92/03/25 11:15:52 ouster Exp $ (Berkeley)
  17.  
  18. if {[string compare test [info procs test]] == 1} then {source defs}
  19.  
  20. catch {exec rm -f test1 test2 test3}
  21. exec cat > test1 << "Two lines: this one\nand this one\n"
  22. exec cat > test2 << "line1\nline2\nline3\nline4\nline5\n"
  23.  
  24. test open-1.1 {open command (files only)} {
  25.     set f [open test1]
  26.     set x [gets $f]
  27.     close $f
  28.     set x
  29. } {Two lines: this one}
  30. test open-1.2 {open command (files only)} {
  31.     set f [open test1]
  32.     set f2 [open test2]
  33.     set f3 [open test1]
  34.     set f4 [open test1]
  35.     set x [list [gets $f] [gets $f2] [gets $f3] [gets $f4] \
  36.         [gets $f] [gets $f2]]
  37.     close $f
  38.     close $f2
  39.     close $f3
  40.     close $f4
  41.     set x
  42. } {{Two lines: this one} line1 {Two lines: this one} {Two lines: this one} {and this one} line2}
  43. test open-1.3 {open command (files only)} {
  44.     set f [open test3 w]
  45.     puts $f xyz
  46.     close $f
  47.     exec cat test3
  48. } "xyz"
  49. test open-1.4 {open command (files only)} {
  50.     set f [open test3 w]
  51.     puts $f xyz
  52.     close $f
  53.     set f [open test3 a]
  54.     puts $f 123
  55.     close $f
  56.     exec cat test3
  57. } "xyz\n123"
  58. test open-1.5 {open command (files only)} {
  59.     set f [open test3 w]
  60.     puts $f xyz\n123
  61.     close $f
  62.     set f [open test3 r+]
  63.     set x [gets $f]
  64.     seek $f 0 current
  65.     puts $f 456
  66.     close $f
  67.     list $x [exec cat test3]
  68. } "xyz {xyz
  69. 456}"
  70. test open-1.6 {open command (files only)} {
  71.     set f [open test3 w]
  72.     puts $f xyz\n123
  73.     close $f
  74.     set f [open test3 w+]
  75.     puts $f xyzzy
  76.     seek $f 2
  77.     set x [gets $f]
  78.     close $f
  79.     list $x [exec cat test3]
  80. } "zzy xyzzy"
  81. test open-1.7 {open command (files only)} {
  82.     set f [open test3 w]
  83.     puts $f xyz\n123
  84.     close $f
  85.     set f [open test3 a+]
  86.     puts $f xyzzy
  87.     flush $f
  88.     set x [tell $f]
  89.     seek $f -4 cur
  90.     set y [gets $f]
  91.     close $f
  92.     list $x [exec cat test3] $y
  93. } {14 {xyz
  94. 123
  95. xyzzy} zzy}
  96.  
  97. test open-2.1 {errors in open command} {
  98.     list [catch {open} msg] $msg
  99. } {1 {wrong # args: should be "open filename ?access?"}}
  100. test open-2.2 {errors in open command} {
  101.     list [catch {open a b c} msg] $msg
  102. } {1 {wrong # args: should be "open filename ?access?"}}
  103. test open-2.3 {errors in open command} {
  104.     list [catch {open test1 x} msg] $msg
  105. } {1 {illegal access mode "x"}}
  106. test open-2.4 {errors in open command} {
  107.     list [catch {open test1 rw} msg] $msg
  108. } {1 {illegal access mode "rw"}}
  109. test open-2.5 {errors in open command} {
  110.     list [catch {open test1 r+1} msg] $msg
  111. } {1 {illegal access mode "r+1"}}
  112. test open-2.6 {errors in open command} {
  113.     string tolower [list [catch {open _non_existent_} msg] $msg $errorCode]
  114. } {1 {couldn't open "_non_existent_": no such file or directory} {unix enoent {no such file or directory}}}
  115.  
  116. if {![file exists ~/_test_] && [file writable ~]} {
  117.     test open-3.1 {tilde substitution in open} {
  118.     set f [open ~/_test_ w]
  119.     puts $f "Some text"
  120.     close $f
  121.     set x [file exists $env(HOME)/_test_]
  122.     exec rm -f $env(HOME)/_test_
  123.     set x
  124.     } 1
  125. }
  126. test open-3.2 {tilde substitution in open} {
  127.     set home $env(HOME)
  128.     unset env(HOME)
  129.     set x [list [catch {open ~/foo} msg] $msg]
  130.     set env(HOME) $home
  131.     set x
  132. } {1 {couldn't find HOME environment variable to expand "~/foo"}}
  133.  
  134. test open-4.1 {file id parsing errors} {
  135.     list [catch {eof gorp} msg] $msg $errorCode
  136. } {1 {bad file identifier "gorp"} NONE}
  137. test open-4.2 {file id parsing errors} {
  138.     list [catch {eof filex} msg] $msg
  139. } {1 {bad file identifier "filex"}}
  140. test open-4.3 {file id parsing errors} {
  141.     list [catch {eof file12a} msg] $msg
  142. } {1 {bad file identifier "file12a"}}
  143. test open-4.4 {file id parsing errors} {
  144.     list [catch {eof file123} msg] $msg
  145. } {1 {file "file123" isn't open}}
  146. test open-4.5 {file id parsing errors} {
  147.     list [catch {eof file1} msg] $msg
  148. } {0 0}
  149. test open-4.5 {file id parsing errors} {
  150.     list [catch {eof stdin} msg] $msg
  151. } {0 0}
  152. test open-4.6 {file id parsing errors} {
  153.     list [catch {eof stdout} msg] $msg
  154. } {0 0}
  155. test open-4.7 {file id parsing errors} {
  156.     list [catch {eof stderr} msg] $msg
  157. } {0 0}
  158. test open-4.8 {file id parsing errors} {
  159.     list [catch {eof stderr1} msg] $msg
  160. } {1 {bad file identifier "stderr1"}}
  161. set f [open test1]
  162. close $f
  163. set expect "1 {file \"$f\" isn't open}"
  164. test open-4.9 {file id parsing errors} {
  165.     list [catch {eof $f} msg] $msg
  166. } $expect
  167.  
  168. test open-5.1 {close command (files only)} {
  169.     list [catch {close} msg] $msg $errorCode
  170. } {1 {wrong # args: should be "close fileId"} NONE}
  171. test open-5.2 {close command (files only)} {
  172.     list [catch {close a b} msg] $msg $errorCode
  173. } {1 {wrong # args: should be "close fileId"} NONE}
  174. test open-5.3 {close command (files only)} {
  175.     list [catch {close gorp} msg] $msg $errorCode
  176. } {1 {bad file identifier "gorp"} NONE}
  177. test open-5.4 {close command (files only)} {
  178.     list [catch {close file4} msg] \
  179.         [string range $msg [string first {" } $msg] end] $errorCode
  180. } {1 {" isn't open} NONE}
  181.  
  182. test open-6.1 {puts command} {
  183.     list [catch {puts file3} msg] $msg $errorCode
  184. } {1 {wrong # args: should be "puts fileId string ?nonewline?"} NONE}
  185. test open-6.2 {puts command} {
  186.     list [catch {puts a b c d} msg] $msg $errorCode
  187. } {1 {wrong # args: should be "puts fileId string ?nonewline?"} NONE}
  188. test open-6.3 {puts command} {
  189.     list [catch {puts a b nonewlinx} msg] $msg $errorCode
  190. } {1 {bad argument "nonewlinx": should be "nonewline"} NONE}
  191. test open-6.4 {puts command} {
  192.     list [catch {puts gorp "New text"} msg] $msg $errorCode
  193. } {1 {bad file identifier "gorp"} NONE}
  194. test open-6.5 {puts command} {
  195.     set f [open test3]
  196.     set x [list [catch {puts $f "New text"} msg] \
  197.     [string range $msg [string first " " $msg] end] $errorCode]
  198.     close $f
  199.     set x
  200. } {1 { wasn't opened for writing} NONE}
  201. test open-6.6 {puts command} {
  202.     set f [open test3 w]
  203.     puts $f "Text1" n
  204.     puts $f " Text 2" no
  205.     puts $f " Text 3"
  206.     close $f
  207.     exec cat test3
  208. } {Text1 Text 2 Text 3}
  209.  
  210. test open-7.1 {gets command} {
  211.     list [catch {gets} msg] $msg $errorCode
  212. } {1 {wrong # args: should be "gets fileId ?varName?"} NONE}
  213. test open-7.2 {gets command} {
  214.     list [catch {gets a b c} msg] $msg $errorCode
  215. } {1 {wrong # args: should be "gets fileId ?varName?"} NONE}
  216. test open-7.3 {gets command} {
  217.     list [catch {gets a} msg] $msg $errorCode
  218. } {1 {bad file identifier "a"} NONE}
  219. test open-7.4 {gets command} {
  220.     set f [open test3 w]
  221.     set x [list [catch {gets $f} msg] \
  222.         [string range $msg [string first " " $msg] end] $errorCode]
  223.     close $f
  224.     set x
  225. } {1 { wasn't opened for reading} NONE}
  226. set f [open test3 w]
  227. puts $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" nonewline
  228. puts $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" nonewline
  229. puts $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" nonewline
  230. puts $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" nonewline
  231. puts $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  232. close $f
  233. test open-7.5 {gets command with long line} {
  234.     set f [open test3]
  235.     set x [gets $f]
  236.     close $f
  237.     set x
  238. } {abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ}
  239. test open-7.6 {gets command with long line} {
  240.     set f [open test3]
  241.     set x [gets $f y]
  242.     close $f
  243.     list $x $y
  244. } {260 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ}
  245. test open-7.7 {gets command and end of file} {
  246.     set f [open test3 w]
  247.     puts $f "Test1\nTest2" nonewline
  248.     close $f
  249.     set f [open test3]
  250.     set x {}
  251.     set y {}
  252.     lappend x [gets $f y] $y
  253.     set y {}
  254.     lappend x [gets $f y] $y
  255.     set y {}
  256.     lappend x [gets $f y] $y
  257.     close $f
  258.     set x
  259. } {5 Test1 5 Test2 -1 {}}
  260. set f [open test3 w]
  261. puts $f "Line 1"
  262. puts $f "Line 2"
  263. test open-7.8 {gets command and bad variable} {
  264.     catch {unset x}
  265.     set x 24
  266.     set f [open test3 r]
  267.     set result [list [catch {gets $f x(0)} msg] $msg]
  268.     close $f
  269.     set result
  270. } {1 {can't set "x(0)": variable isn't array}}
  271.  
  272. test open-8.1 {read command} {
  273.     list [catch {read} msg] $msg $errorCode
  274. } {1 {wrong # args: should be "read fileId ?numBytes|nonewline?"} NONE}
  275. test open-8.2 {read command} {
  276.     list [catch {read a b c} msg] $msg $errorCode
  277. } {1 {wrong # args: should be "read fileId ?numBytes|nonewline?"} NONE}
  278. test open-8.3 {read command} {
  279.     list [catch {read file10} msg] $msg $errorCode
  280. } {1 {file "file10" isn't open} NONE}
  281. test open-8.4 {read command} {
  282.     set f [open test3 w]
  283.     set x [list [catch {read $f} msg] \
  284.         [string range $msg [string first " " $msg] end] $errorCode]
  285.     close $f
  286.     set x
  287. } {1 { wasn't opened for reading} NONE}
  288. test open-8.5 {read command} {
  289.     set f [open test1]
  290.     set x [list [catch {read $f 12z} msg] $msg $errorCode]
  291.     close $f
  292.     set x
  293. } {1 {expected integer but got "12z"} NONE}
  294. test open-8.6 {read command} {
  295.     set f [open test1]
  296.     set x [list [catch {read $f z} msg] $msg $errorCode]
  297.     close $f
  298.     set x
  299. } {1 {bad argument "z": should be "nonewline"} NONE}
  300. test open-8.7 {read command} {
  301.     set f [open test1]
  302.     set x [list [read $f 1] [read $f 2] [read $f]]
  303.     close $f
  304.     set x
  305. } {T wo { lines: this one
  306. and this one
  307. }}
  308. test open-8.8 {read command, with over-large count} {
  309.     set f [open test1]
  310.     set x [read $f 100]
  311.     close $f
  312.     set x
  313. } {Two lines: this one
  314. and this one
  315. }
  316. test open-8.9 {read command, nonewline option} {
  317.     set f [open test1]
  318.     set x [read $f n]
  319.     close $f
  320.     set x
  321. } {Two lines: this one
  322. and this one}
  323.  
  324. test open-9.1 {seek command} {
  325.     list [catch {seek foo} msg] $msg $errorCode
  326. } {1 {wrong # args: should be "seek fileId offset ?origin?"} NONE}
  327. test open-9.2 {seek command} {
  328.     list [catch {seek foo a b c} msg] $msg $errorCode
  329. } {1 {wrong # args: should be "seek fileId offset ?origin?"} NONE}
  330. test open-9.3 {seek command} {
  331.     list [catch {seek foo 0} msg] $msg $errorCode
  332. } {1 {bad file identifier "foo"} NONE}
  333. test open-9.4 {seek command} {
  334.     set f [open test2]
  335.     set x [list [catch {seek $f xyz} msg] $msg $errorCode]
  336.     close $f
  337.     set x
  338. } {1 {expected integer but got "xyz"} NONE}
  339. test open-9.5 {seek command} {
  340.     set f [open test2]
  341.     set x [list [catch {seek $f 100 gorp} msg] $msg $errorCode]
  342.     close $f
  343.     set x
  344. } {1 {bad origin "gorp": should be start, current, or end} NONE}
  345. set f [open test3 w]
  346. puts $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" nonewline
  347. close $f
  348. test open-9.6 {seek command} {
  349.     set f [open test3]
  350.     set x [read $f 1]
  351.     seek $f 3
  352.     lappend x [read $f 1]
  353.     seek $f 0 start
  354.     lappend x [read $f 1]
  355.     seek $f 10 current
  356.     lappend x [read $f 1]
  357.     seek $f -2 end
  358.     lappend x [read $f 1]
  359.     seek $f 50 end
  360.     lappend x [read $f 1]
  361.     seek $f 1
  362.     lappend x [read $f 1]
  363.     close $f
  364.     set x
  365. } {a d a l Y {} b}
  366.  
  367. test open-10.1 {tell command} {
  368.     list [catch {tell} msg] $msg $errorCode
  369. } {1 {wrong # args: should be "tell fileId"} NONE}
  370. test open-10.2 {tell command} {
  371.     list [catch {tell a b} msg] $msg $errorCode
  372. } {1 {wrong # args: should be "tell fileId"} NONE}
  373. test open-10.3 {tell command} {
  374.     list [catch {tell a} msg] $msg $errorCode
  375. } {1 {bad file identifier "a"} NONE}
  376. test open-10.4 {tell command} {
  377.     set f [open test2]
  378.     set x [tell $f]
  379.     read $f 3
  380.     lappend x [tell $f]
  381.     seek $f 2
  382.     lappend x [tell $f]
  383.     seek $f 10 current
  384.     lappend x [tell $f]
  385.     seek $f 0 end
  386.     lappend x [tell $f]
  387.     close $f
  388.     set x
  389. } {0 3 2 12 30}
  390.  
  391. test open-11.1 {eof command} {
  392.     list [catch {eof} msg] $msg $errorCode
  393. } {1 {wrong # args: should be "eof fileId"} NONE}
  394. test open-11.2 {eof command} {
  395.     list [catch {eof a b} msg] $msg $errorCode
  396. } {1 {wrong # args: should be "eof fileId"} NONE}
  397. test open-11.3 {eof command} {
  398.     list [catch {eof file100} msg] $msg $errorCode
  399. } {1 {file "file100" isn't open} NONE}
  400. test open-11.4 {eof command} {
  401.     set f [open test1]
  402.     set x [eof $f]
  403.     lappend x [eof $f]
  404.     gets $f
  405.     lappend x [eof $f]
  406.     gets $f
  407.     lappend x [eof $f]
  408.     gets $f
  409.     lappend x [eof $f]
  410.     lappend x [eof $f]
  411.     close $f
  412.     set x
  413. } {0 0 0 0 1 1}
  414.  
  415. test open-12.1 {flush command} {
  416.     list [catch {flush} msg] $msg $errorCode
  417. } {1 {wrong # args: should be "flush fileId"} NONE}
  418. test open-12.2 {flush command} {
  419.     list [catch {flush a b} msg] $msg $errorCode
  420. } {1 {wrong # args: should be "flush fileId"} NONE}
  421. test open-12.3 {flush command} {
  422.     list [catch {flush a} msg] $msg $errorCode
  423. } {1 {bad file identifier "a"} NONE}
  424. test open-12.4 {flush command} {
  425.     set f [open test3]
  426.     set x [list [catch {flush $f} msg] \
  427.         [string range $msg [string first " " $msg] end] $errorCode]
  428.     close $f
  429.     set x
  430. } {1 { wasn't opened for writing} NONE}
  431. test open-12.5 {flush command} {
  432.     set f [open test3 w]
  433.     puts $f "Line 1"
  434.     puts $f "Line 2"
  435.     set f2 [open test3]
  436.     set x {}
  437.     lappend x [read $f2 nonewline]
  438.     close $f2
  439.     flush $f
  440.     set f2 [open test3]
  441.     lappend x [read $f2 nonewline]
  442.     close $f2
  443.     close $f
  444.     set x
  445. } {{} {Line 1
  446. Line 2}}
  447.  
  448. test open-13.1 {I/O to command pipelines} {
  449.     list [catch {open "| cat < test1 > test3" w} msg] $msg $errorCode
  450. } {1 {can't write input to command: standard input was redirected} NONE}
  451. test open-13.2 {I/O to command pipelines} {
  452.     list [catch {open "| echo > test3" r} msg] $msg $errorCode
  453. } {1 {can't read output from command: standard output was redirected} NONE}
  454. test open-13.3 {I/O to command pipelines} {
  455.     list [catch {open "| echo > test3" r+} msg] $msg $errorCode
  456. } {1 {can't read output from command: standard output was redirected} NONE}
  457. test open-13.4 {writing to command pipelines} {
  458.     exec rm test3
  459.     set f [open "| cat | cat > test3" w]
  460.     puts $f "Line 1"
  461.     puts $f "Line 2"
  462.     close $f
  463.     exec cat test3
  464. } {Line 1
  465. Line 2}
  466. test open-13.5 {reading from command pipelines} {
  467.     set f [open "| cat test2" r]
  468.     set x [list [gets $f] [gets $f] [gets $f]]
  469.     close $f
  470.     set x
  471. } {line1 line2 line3}
  472. test open-13.6 {both reading and writing from/to command pipelines} {
  473.     set f [open "| cat -u" r+]
  474.     puts $f "Line1"
  475.     flush $f
  476.     set x [gets $f]
  477.     close $f
  478.     set x
  479. } {Line1}
  480. test open-13.7 {errors in command pipelines} {
  481.     set f [open "|gorp"]
  482.     list [catch {close $f} msg] $msg [lindex $errorCode 0] [lindex $errorCode 2]
  483. } {1 {couldn't find "gorp" to execute} CHILDSTATUS 1}
  484. test open-13.8 {errors in command pipelines} {
  485.     set f [open "|gorp" w]
  486.     exec sleep 1
  487.     puts $f output
  488.     set x [list [catch {flush $f} msg] [concat \
  489.         [string range $msg 0 [string first {"} $msg]] \
  490.         [string range $msg [string first : $msg] end]] $errorCode]
  491.     catch {close $f}
  492.     string tolower $x
  493. } {1 {error flushing " : broken pipe} {unix epipe {broken pipe}}}
  494. test open-13.9 {errors in command pipelines} {
  495.     set f [open "|gorp" w]
  496.     list [catch {close $f} msg] $msg \
  497.         [lindex $errorCode 0] [lindex $errorCode 2]
  498. } {1 {couldn't find "gorp" to execute} CHILDSTATUS 1}
  499. test open-13.10 {errors in command pipelines} {
  500.     set f [open "|gorp" w]
  501.     exec sleep 1
  502.     puts $f output
  503.     string tolower [list [catch {close $f} msg] [concat \
  504.         [string range $msg 0 [string first {"} $msg]] \
  505.         [string range $msg [string first : $msg] end]] \
  506.         [lindex $errorCode 0] [lindex $errorCode 2]]
  507. } {1 {error closing " : broken pipe
  508. couldn't find "gorp" to execute} childstatus 1}
  509.  
  510. catch {exec rm -f test1 test2 test3}
  511. concat {}
  512.